package com.ruoyi.produce.base.shift.util;

import java.sql.*;

public class SqlConnect {

    private Connection conn=null;
    PreparedStatement statement=null;
    public void connsql(String url) {

        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=(Connection) DriverManager.getConnection(url);
            System.out.println("数据库连接成功");
        }catch(ClassNotFoundException cnfex){
            System.err.println("装载JDBC驱动器失败");
            cnfex.printStackTrace();
        }catch(SQLException sqlex){
            System.err.println("无法连接数据库");
            sqlex.printStackTrace();
        }

    }
    public void deconnsql(){
        try{
            if(conn!=null)
                conn.close();

        }catch(Exception e){
            System.out.println("关闭数据库问题");
            e.printStackTrace();
        }
    }
    public ResultSet selectsql(String sql){
        ResultSet rs =null;
        try{
            statement=conn.prepareStatement(sql);
            rs= statement.executeQuery(sql);

        }catch(SQLException e){
            e.printStackTrace();

        }
        return rs;
    }
    public void insertsql(String sql)
    {
        try{
            statement=conn.prepareStatement(sql);
            statement.executeUpdate();

        }catch (SQLException e) {
            System.out.println("插入数据库时出错：");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("插入时出错：");
            e.printStackTrace();
        }


    }
    public void deletesql(String sql){
        try{
            statement=conn.prepareStatement(sql);
            statement.executeUpdate();

        } catch(SQLException e){
            System.out.println("删除数据库时出错");
            e.printStackTrace();
        }catch(Exception e){
            System.out.println("删除数据时出错：");
            e.printStackTrace();
        }


    }
    public void updatasql(String sql){
        try{
            statement=conn.prepareStatement(sql);
            statement.executeUpdate();

        } catch(SQLException e){
            System.out.println("更新数据库时出错");
            e.printStackTrace();
        }catch(Exception e){
            System.out.println("更新数据时出错：");
            e.printStackTrace();
        }


    }
}
